﻿using System.Xml;

using netaddICTs.Umb.ProtectedMedia.Business.Configuration;

using umbraco.cms.businesslogic.media;
using umbraco.cms.businesslogic.member;

namespace netaddICTs.Umb.ProtectedMedia.Web.XsltExtensions {

    public class Library {
    
        /// <summary>
        /// Checks whether current member has sufficient permissions to download secured media file
        /// </summary>
        /// <param name="mediaId">Id of the media item</param>
        /// <returns>Returns true if current member has sufficient rights, false otherwise</returns>
        public static bool HasPermissionToDownload(int mediaId){
            return HasPermissionToDownload(mediaId, Member.GetCurrentMember());
        }

        /// <summary>
        /// Checks whether current member has sufficient permissions to download secured media file
        /// </summary>
        /// <param name="mediaId">Id of media item</param>
        /// <param name="member"><see cref="Member"/>Member</param> instance
        /// <returns></returns>
        public static bool HasPermissionToDownload(int mediaId, Member member){
            if (member == null) return false;

            //Get groups for current member
            var groupsForCurrentMember = member.Groups;
            //Get media item associated with supplied media ie
            var media = new Media(mediaId);

            try {
                //Get parent media item; will throw ArgumentException if parent media item does not exist
                var parentMedia = new Media(media.Parent.Id);
                //Get permission settings; will only be available in case of a 'Protected Folder' document
                var memberGroupsXml =
                    parentMedia.getProperty(
                        Business.Configuration.ProtectedMedia.Instance.Aliases.PermissionAlias.UmbracoAlias).Value.
                        ToString();

                //Check if we're actually dealing with a 'Protected Folder'
                if (!string.IsNullOrEmpty(memberGroupsXml)) {
                    var document = new XmlDocument();

                    document.LoadXml(memberGroupsXml);

                    var memberGroupsNodeList = document.SelectNodes(Constants.PermissionsXPath);

                    //Check permissions for current member
                    if (memberGroupsNodeList != null)
                        foreach (XmlNode memberGroupNode in memberGroupsNodeList) {
                            var memberGroupId =
                                int.Parse(memberGroupNode.Attributes[Constants.PermissionsIdAttribute].Value);

                            if (groupsForCurrentMember.ContainsKey(memberGroupId))
                                return true;
                        }
                }
            } catch {
                return false;
            }

            return false;
        }

        /// <summary>
        /// Constructs a download link for any media item
        /// </summary>
        /// <param name="mediaId">Id of the media item</param>
        /// <returns>Download link for media item</returns>
        public static string GetDownloadLink(int mediaId){
            try {
                var mediaItem = new Media(mediaId);

                return mediaItem.getProperty("umbracoFile").Value.ToString();
            } catch {
                return string.Empty;
            }
        }
    }
}